package cn.edu.lzzy.controllers.admin;


import cn.edu.lzzy.entities.Messages;
import cn.edu.lzzy.entities.User;
import cn.edu.lzzy.services.UserService;
import cn.edu.lzzy.vm.JSONResult;
import cn.edu.lzzy.vm.VmUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.oas.annotations.EnableOpenApi;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/***
 * 系统管理员业务
 * 包含用户查询、用户分页查询、教师申请列表、审批教师申请、禁用用户业务
 */
@CrossOrigin
@RestController
@EnableOpenApi
@Tag(name = "user-api-controller",description = "业务管理员接口实现")
public class UserApiController {
    private  final UserService userService;

    @Autowired
    public UserApiController(UserService userService){
        this.userService=userService;
    }
    /*
    查询用户列表
//     */
    @Operation(summary = "查询用户列表,调用方法：getUsers(<token>)")
    @Parameters(@Parameter(description = "入口参数User对象的token数据"))
    @GetMapping(value = "/admin")
    public JSONResult getUsers(@RequestParam String token){
        List<VmUser> users=new ArrayList<>();
        userService.getUsers().forEach(u ->users.add(VmUser.create(u,true)));
        return new JSONResult(200,users, Messages.SUCCESS.toString()+Messages.COUNT_VALUE.toString()+users.size());
    }

      /*
    用户分页查询
     */
      @Operation(summary = " 用户分页查询,调用方法：getPageUsers(<token>)")
    @GetMapping(value = "/admin/page")
    public JSONResult getPageUsers(@RequestParam String token,@RequestParam int page,@RequestParam int size){
        List<VmUser> users=new ArrayList<>();
        userService.getUsers(page,size).forEach(u ->users.add(VmUser.create(u,true)));
        return new JSONResult(200,users, Messages.SUCCESS.toString()+",页数：..."+page);
    }


      /*
    查询用户关键字查询列表
    根据keyword查找用户信息
     */
      @Operation(summary = " 查询用户关键字查询,调用方法：search(<keyword>)")
    @GetMapping(value = "/admin/search{keyword}")
    public JSONResult search(@RequestParam String token,@PathVariable String keyword){
        List<VmUser> users=new ArrayList<>();
        userService.search(keyword).forEach(u ->users.add(VmUser.create(u,true)));
        return new JSONResult(200,users, Messages.SUCCESS.toString()+Messages.COUNT_VALUE.toString()+users.size());
    }

        /*
    查找教师申请信息
     */

    @Operation(summary = " 查找教师申请信息,调用方法：getApplyTeacher(<token>)")
    @GetMapping(value = "/admin/applying")
    public JSONResult getApplyTeacher(@RequestParam String token){
        List<VmUser> users=new ArrayList<>();
        userService.getByApplyTeacher(true).forEach(u ->users.add(VmUser.create(u,true)));
        return new JSONResult(200,users, Messages.SUCCESS.toString()+Messages.COUNT_VALUE.toString()+users.size());
    }

        /*
    审批通过教师申请
    如果需要审批某个一个教师申请，需要传入该教师的id值
     */
        @Operation(summary = " 查找教师申请信息,调用方法：getApproveTeacher(<id>)")
    @PostMapping(value = "/admin/approve")
    public JSONResult getApproveTeacher(@RequestParam String token,@RequestParam UUID id){
      User teacher=userService.approveTeacher(id);
      if(teacher==null){
          return new JSONResult(404,null,Messages.WRONG_ID.toString());
      }

        return new JSONResult(200,VmUser.create(teacher,true), Messages.SUCCESS.toString());
    }

      /*
    拒绝教师申请
    如果需要审批某个一个教师申请，需要传入该教师的id值
     */
      @Operation(summary = " 拒绝教师申请,调用方法：declineTeacher(<id>)")
    @PostMapping(value = "/admin/decline")
    public JSONResult declineTeacher(@RequestParam String token,@RequestParam UUID id){
        User teacher=userService.declineTeacher(id);
        if(teacher==null){
            return new JSONResult(404,null,Messages.WRONG_ID.toString());
        }

        return new JSONResult(200,VmUser.create(teacher,false), Messages.SUCCESS.toString());

    }

  /*
  禁止用户
    如果需要禁止某个一个用户，需要传入该用户的id值
     */
  @Operation(summary = " 禁止用户,调用方法：banUser(<id>)")
    @PostMapping(value = "/admin/banUser")
    public JSONResult banUser(@RequestParam String token,@RequestParam UUID id){
        User teacher=userService.banUser(id);
        if(teacher==null){
            return new JSONResult(404,null,Messages.WRONG_ID.toString());
        }

        return new JSONResult(200,VmUser.create(teacher,false), Messages.SUCCESS.toString());


    }




}

